﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;

namespace Kugar.MVC.MobileUI.WeUI.Helpers
{
    public static class GlobalConfigurationHelper
    {
        /// <summary>
        /// 启用AdminLTEUI组件
        /// </summary>
        /// <param name="services"></param>
        public static void UseWeUI(this IServiceCollection services)
        {
            services.AddScoped(typeof(MobileWeUI));

            var embeddedProvider = new EmbeddedFileProvider(typeof(MobileWeUI).Assembly, "Kugar.MVC.MobileUI.WeUI");

            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.FileProviders.Add(embeddedProvider);
            });
            
        }

        /// <summary>
        /// 是否使用内嵌的静态资源
        /// </summary>
        /// <param name="app"></param>
        public static void UseWeUIStaticFile(this IApplicationBuilder app)
        {
            var embeddedProvider = new EmbeddedFileProvider(typeof(MobileWeUI).Assembly, "Kugar.MVC.MobileUI.WeUI");

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = embeddedProvider,
                RequestPath = new PathString("/weuibase"),
                OnPrepareResponse = (context) => {
                    var headers = context.Context.Response.GetTypedHeaders();
                    headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
                    {
                        Public = true,
                        MaxAge = TimeSpan.FromDays(365),

                    };
                }
            });
            
        }

    }
}
